<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>尾递归</title>
	</head>
	<body>
		<script>
			/*尾调用优化*/
			
			//原函数：，多次调用自身，多重调用
			factorial(n) {
			  if (n === 1) return 1;
			  return n * factorial(n - 1);
			}
			factorial(5) // 120
			
			//尾递归之后，单次调用自身
			function factorial(n, total) {
			  if (n === 1) return total;
			  return factorial(n - 1, n * total);
			}
			
			factorial(5, 1) // 120
		</script>
	</body>
</html>
